<template>
  <el-tree-select :default-expand-all="props.expandAll==1" :multiple="props.multiple == 1" v-model="name" :data="trees" :render-after-expand="false" :props="defaultProps" accordion :check-strictly="true" :disabled="props.disabled==1" />
</template>

<script setup lang="ts">
import { computed, onMounted } from "vue";
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
let props = defineProps(["modelValue", "expandAll", "multiple", "disabled"]);
const emit = defineEmits(["update:modelValue"]);
const name = computed({
  get() {
    return props.modelValue;
  },
  set(value: Number) {
    emit("update:modelValue", value);
  },
});
let trees = ref([]);
onMounted(() => {
  getInfo();
});
let defaultProps = computed(() => {
  return {
    label: "menuName",
    value: "_id",
  };
});
let getInfo = async () => {
  let resp = await proxy.$apis.menus({ type: "list" });
  let items = proxy.$utils.transformArray(resp, "_id", "parentId");
  trees.value = sortItems(items, "asc", "orderNum");
};
//字段排序
function sortItems(items: Item[], sortOrder: "asc" | "desc", field): Item[] {
  return items
    .sort((a, b) => {
      if (sortOrder === "asc") {
        return a[field] - b[field];
      } else {
        return b[field] - a[field];
      }
    })
    .map((item) => {
      if (item.children) {
        item.children = sortItems(item.children, sortOrder, field);
      }
      return item;
    });
}
</script>

<style lang="scss" scoped>
</style>
